package com.unf.edu.rms.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unf.edu.rms.dao.UserLogin;
import com.unf.edu.rms.util.Password;

@Service
public class UserLoginService {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Transactional
	public boolean validateUser(String userName,String password) throws Exception
	{
		System.out.println("UserLoginService.validateUser()============="+sessionFactory.getCurrentSession());
		//System.out.println("UserLoginService.validateUser()====current Session===="+HibernateSessionFactory.getSessionFactory().getCurrentSession());
		Session session = sessionFactory.getCurrentSession();
		Password pw = new Password();
		password = pw.encryptPassword(password);
		String hql = "FROM UserLogin where userName=:userName and password=:password";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		query.setString("password", password);
		List <UserLogin> userLoginList = query.list();
		if(userLoginList.size()>0)
			System.out.println("UserLoginService.validateUser()=====username====="+userLoginList.get(0).getUserName());
		//System.out.println("UserLoginService.validateUser()=====password====="+userLoginList.get(0).getPassword());
		if(userLoginList.size()==1)
			return true;
		else
			return false;
	}
	
	@Transactional
	public int getUserRole(String userName)
	{
		int roleId = 0;
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userName=:userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			UserLogin userLogin = (UserLogin)userLoginList.get(i);
			roleId= userLogin.getUserRole().getUserRoleId();
			System.out.println("UserLoginService.getUserRole()=====rolename:roleid=========="+userLogin.getUserRole().getRoleName()+":"+userLogin.getUserRole().getUserRoleId());
		}
		System.out.println("UserLoginService.getUserRole()");
		return roleId;
	}
	
	
	@Transactional
	public int getUserFacultyId(String userName)
	{
		int loginFacultyId = 0;
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userName=:userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			UserLogin userLogin = (UserLogin)userLoginList.get(i);
			loginFacultyId= userLogin.getFaculty().getFacultyId();
		}
		return loginFacultyId;
	}
	
	@Transactional
	public int getUserLoginId(String userName){
		
		int userLoginId = 0;
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userName=:userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			UserLogin userLogin = (UserLogin)userLoginList.get(i);
			userLoginId= userLogin.getUserLoginId();
		}
		return userLoginId;
	}
	
	@Transactional
	public UserLogin getUserLogin(String userName){
		
		UserLogin userLogin = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userName=:userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			userLogin = (UserLogin)userLoginList.get(i);
			
		}
		return userLogin;
	}
	
	@Transactional
	public UserLogin getUserLoginById(int userLoginId){
		
		UserLogin userLogin = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userLoginId=:userLoginId";
		Query query = session.createQuery(hql);
		query.setString("userLoginId", Integer.toString(userLoginId));
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			userLogin = (UserLogin)userLoginList.get(i);
			
		}
		return userLogin;
	}
	
	@Transactional
	public boolean getFirstTimeLogin(String userName){
		boolean firstTimeLogin = false;
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userName=:userName";
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List <UserLogin> userLoginList = query.list();
		for(int i=0;i<userLoginList.size();i++)
		{
			UserLogin userLogin = (UserLogin)userLoginList.get(i);
			String firstTimeLoginString = userLogin.getFirstTimeLogin();
			System.out.println("firstTimeLoginString " + firstTimeLoginString);
			if(firstTimeLoginString.equals("y") || firstTimeLoginString.equals("Y")){
				firstTimeLogin = true;
			}
		}
		System.out.println("First Time Login Check" + firstTimeLogin);
		return firstTimeLogin;
	}
	
	/*public void validateUser()
	{
		System.out.println("UserLoginService.validateUser()============="+sessionFactory.getCurrentSession());
		//System.out.println("UserLoginService.validateUser()====current Session===="+HibernateSessionFactory.getSessionFactory().getCurrentSession());
		List<UserLogin> userLoginList = sessionFactory.getCurrentSession()
				.createQuery("from UserLogin").list();
		for(int i=0;i<userLoginList.size();i++)
		{
			UserLogin userLogin = (UserLogin)userLoginList.get(i);
			System.out.println("UserLoginService.validateUser()======"+userLogin.getUserName());
		}
		
	}*/
	 
}
